package com.bsoft.exchange.webservice.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jws.WebService;

import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bsoft.exchange.domain.PatientDomain;
import com.bsoft.exchange.domain.RegisterDomain;
import com.bsoft.exchange.response.ResponseEntity;
import com.bsoft.exchange.service.PatientService;
import com.bsoft.exchange.service.RegisterService;
import com.bsoft.exchange.util.BaseResponse;
import com.bsoft.exchange.util.Constant;
import com.bsoft.exchange.util.DateUtil;
import com.bsoft.exchange.vo.CardVo;
import com.bsoft.exchange.vo.PatientCardVo;
import com.bsoft.exchange.vo.PatientVo;
import com.bsoft.exchange.vo.RegisterVo;
import com.bsoft.exchange.webservice.RegisterWebService;

@WebService(serviceName="RegisterService",//对外发布的服务名
                     targetNamespace="http://webservice.exchange.bsoft.com",//指定你想要的名称空间，通常使用使用包名反转
                     endpointInterface="com.bsoft.exchange.webservice.RegisterWebService")//服务接口全路径, 指定做SEI（Service EndPoint Interface）服务端点接口
 @Component
public class RegisterWebServiceImpl implements RegisterWebService {
	
	@Autowired
    Mapper dozerMapper;
	
	@Autowired
	RegisterService registerService;
	
	@Autowired
	PatientService patientService;
	
    @Override
    public ResponseEntity<RegisterVo>  getRegistrationForm(String hospitalCode, String hisOrderNumber) {
    	ResponseEntity<RegisterVo> entry = new ResponseEntity();
		
		System.out.println("根据机构id【"+hospitalCode+"】，挂号流水号【"+hisOrderNumber+"】获取挂号单信息");

		RegisterDomain register = registerService.getRegistrationForm( hospitalCode, hisOrderNumber);
		
		//实体bean转换
		if(register!=null){
			RegisterVo registerVo = dozerMapper.map(register, RegisterVo.class);
			entry.setData(registerVo);
		}

		return entry;
    }
    @Override
    public ResponseEntity<List<PatientVo>> getPhoneAndCards(
			String hospitalCode,
			Integer patientIdentityCardType,
			String patientIdentityCardNumber,
			Integer patientMedicalCardType,
			String patientMedicalCardNumber,
			String patientName,
			Integer patientSex,
			String patientBirthday,
			String patientMobile,
			String provinceCode,
			String cityCode,
			String areaCode,
			String patientAddress,
			Integer operationType
			) {
		
		ResponseEntity<List<PatientVo>> entry = new ResponseEntity();

		Map<String, Object> ret = new HashMap<String, Object>();
		
		ret = patientService.getPhoneAndCards(  hospitalCode, 
				 patientIdentityCardType,
				 patientIdentityCardNumber,
				 patientMedicalCardType,
				 patientMedicalCardNumber,
				 patientName,
				 patientSex,
				 patientBirthday,
				 patientMobile,
				 provinceCode,
				 cityCode,
				 areaCode,
				 patientAddress,
				 operationType);
		
		List<PatientDomain> patientList = (List<PatientDomain>) ret.get("patientList");
		String ywlx = (String) ret.get("ywlx");
		List<PatientVo> patientVoList = new ArrayList();
		//实体bean转换
		if(patientList!=null && patientList.size() > 0){
			
			
			List<CardVo> cardVoList = new ArrayList();
			
			String phone = "";
			String jzkh;
			String ybkh;
			String zyhm;
			String xzmc;
			Long brid;
			for(PatientDomain patient : patientList){
				
				PatientVo patientVo = new PatientVo();
				
				phone = patient.getLxdh();
				jzkh = patient.getJzkh();
				ybkh = patient.getYbkh();
				brid = patient.getBrid();
				xzmc = patient.getXzmc();
				zyhm = patient.getZyhm();
				if("卡查询".equals(ywlx) && patientMedicalCardType!=null && patientMedicalCardNumber !=null && !"".equals(patientMedicalCardNumber)){
					if(jzkh != null && !"".equals(jzkh) && jzkh.equals(patientMedicalCardNumber)){
						CardVo card = new CardVo();
						card.setPatientMedicalCardType(2);
						card.setPatientMedicalCardNumber(jzkh);
						card.setPatientId(brid.toString());
						card.setPatientNature(xzmc);
						cardVoList.add(card);
					}
					
					if(ybkh != null && !"".equals(ybkh)&& ybkh.equals(patientMedicalCardNumber)){
						CardVo ybkhCard = new CardVo();
						ybkhCard.setPatientMedicalCardType(3);
						ybkhCard.setPatientMedicalCardNumber(ybkh);
						ybkhCard.setPatientId(brid.toString());
						ybkhCard.setPatientNature(xzmc);
						cardVoList.add(ybkhCard);
					}
					
					if(zyhm != null && !"".equals(zyhm)&& zyhm.equals(patientMedicalCardNumber)){
						CardVo zyhmCard = new CardVo();
						zyhmCard.setPatientMedicalCardType(5);
						zyhmCard.setPatientMedicalCardNumber(zyhm);
						zyhmCard.setPatientId(brid.toString());
						zyhmCard.setPatientNature(xzmc);
						cardVoList.add(zyhmCard);
					}
					
				}
				patientVo.setPatientMobile(phone);
				if("建档".equals(ywlx)){
					CardVo jdCard = new CardVo();
					
					jdCard.setPatientId(brid.toString());
					jdCard.setPatientNature(xzmc);
					cardVoList.add(jdCard);	
				}
				patientVo.setListMedicalCard(cardVoList);
				patientVoList.add(patientVo);
			}
			
			
			
		}
		entry.setData(patientVoList);
		return entry;

	}
	
    @Override
	public ResponseEntity<List<PatientCardVo>> queryBusCards(String hospitalCode,String patientCode) {
		
    	ResponseEntity<List<PatientCardVo>> entry = new ResponseEntity();

//		PatientDomain patient = patientService.queryBusCards(  patientCodePojo.getHospitalCode(), patientCodePojo.getPatientCode());
		BaseResponse<List<PatientDomain>> patientListResponse = patientService.queryBusCards(hospitalCode , patientCode);
		
		List<PatientCardVo> patientCardVoList = new ArrayList();
		
		 if (patientListResponse.getCode()==Constant.errorTag){
			 entry.setCode(0);
			 entry.setMessage(patientListResponse.getMessage());
	         return entry;
	     }
		 List<PatientDomain> patientList = patientListResponse.getData();
		//实体bean转换
		if(patientList !=null && patientList.size() > 0){
			
			for(PatientDomain patient : patientList){
				String jzkh;
				String ybkh;
				jzkh = patient.getJzkh();
				ybkh = patient.getYbkh();
				
				if(jzkh != null && !"".equals(jzkh)){
					
					PatientCardVo patientCardVo = new PatientCardVo();
					
					patientCardVo.setPatientBirthday(DateUtil.dateFormat(patient.getCsny()));
					patientCardVo.setPatientMobile(patient.getLxdh());
					patientCardVo.setPatientName(patient.getBrxm());
					patientCardVo.setPatientSex(patient.getBrxb());
					
					
					patientCardVo.setPatientMedicalCardType(2);
					patientCardVo.setPatientMedicalCardNumber(jzkh);
					patientCardVoList.add(patientCardVo);
				}
				
				if(ybkh != null && !"".equals(ybkh)){
					PatientCardVo patientCardVo = new PatientCardVo();
					
					patientCardVo.setPatientBirthday(DateUtil.dateFormat(patient.getCsny()));
					patientCardVo.setPatientMobile(patient.getLxdh());
					patientCardVo.setPatientName(patient.getBrxm());
					patientCardVo.setPatientSex(patient.getBrxb());
					patientCardVo.setPatientMedicalCardType(3);
					patientCardVo.setPatientMedicalCardNumber(ybkh);
					patientCardVoList.add(patientCardVo);
				}
				/*//同时为空的 返回基本信息
				if((ybkh == null || "".equals(ybkh))&&(jzkh == null || "".equals(jzkh))){
					
					PatientCardVo patientCardVo = new PatientCardVo();
					if(patient.getCsny()!=null){
						patientCardVo.setPatientBirthday(DateUtil.dateFormat(patient.getCsny()));
					}
					
					patientCardVo.setPatientMobile(patient.getLxdh());
					patientCardVo.setPatientName(patient.getBrxm());
					patientCardVo.setPatientSex(patient.getBrxb());
					patientCardVoList.add(patientCardVo);
				}*/
			}
			
		}
		entry.setData(patientCardVoList);

		return entry;

	}
    
   
}
